﻿package com.work.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.work.domain.UserAdmin;
import com.work.domain.UserLoginlog;
import com.work.service.UserAdminService;
import com.work.service.UserLogService;
import com.work.util.DateUtil;
import com.work.util.GetLoginLog;
import com.work.util.GetUserSessionTool;

/**
 * 
 * @author zhubingyi
 * @version 1.0
 * 
 */
@Controller
@RequestMapping("/UserAdmin")
public class UserAdminController {
	
	@Resource
	private UserAdminService userAdminService;
	@Resource
	private UserLogService userLogService;
	
	
	//用户登录
	@RequestMapping(value="login",produces="text/html;charset=UTF-8")
	public  void login(@RequestBody UserAdmin user,HttpServletResponse response,HttpServletRequest request){
		HttpSession session = request.getSession();
		String username = user.getAdminname();
		String password = user.getAdminpassword();
		UserAdmin user1=userAdminService.login(username,password);
		System.out.println(user1.getAdminname());
		PrintWriter writer;
		try {
			writer = response.getWriter();
			if(user1==null||"".equals(user1)){
				System.out.println("登录失败！");
				writer.print("0");
			}else{
				System.out.println("登录成功！");
				//查询用户上次登陆日志
				UserLoginlog ul = userLogService .findLoginInfo(username);
				if(ul==null){
					//首次登陆
					UserLoginlog userLog=new UserLoginlog();
					userLog.setUsernamelog(user1.getAdminname());
					userLog.setUserrolelog(user1.getRole());
					userLog.setLoginiplog(GetLoginLog.getUserIp(request));
					userLog.setLogintimelog(GetLoginLog.getDateTime());
					userLog.setOsinfolog(GetLoginLog.getUserOsInfo());
					userLog.setLogincountslog(GetLoginLog.getLoginCounts(0));
					userLog.setDourllog(GetLoginLog.getUserUrl(request));
					userLog.setDorequestlog(GetLoginLog.getDoRequestLog(request));
					int flag=userLogService.addLoginInfo(userLog);
					if(flag==1){
						System.out.println("添加日志成功！");
						//展示登陆ip
						session.setAttribute("adminip", "本次登陆ip:"+request.getLocalAddr());
						//展示登陆时间
						session.setAttribute("admintime", "本次登陆时间:"+GetLoginLog.getDateTime());
						//展示登陆次数
						session.setAttribute("admincounts", GetLoginLog.getLoginCounts(0));
					}else{
						System.out.println("添加日志失败!");
					}
				}else{
					//已经登陆过的用户
					UserLoginlog userLog=new UserLoginlog();
					userLog.setUsernamelog(user1.getAdminname());
					userLog.setUserrolelog(user1.getRole());
					userLog.setLoginiplog(GetLoginLog.getUserIp(request));
					userLog.setLogintimelog(GetLoginLog.getDateTime());
					userLog.setOsinfolog(GetLoginLog.getUserOsInfo());
					userLog.setLogincountslog(GetLoginLog.getLoginCounts(ul.getLogincountslog()));
					userLog.setDourllog(GetLoginLog.getUserUrl(request));
					userLog.setDorequestlog(GetLoginLog.getDoRequestLog(request));
					int flag=userLogService.addLoginInfo(userLog);
					if(flag==1){
						System.out.println("添加日志成功！");
						//展示登陆ip
						session.setAttribute("adminip", "上次登陆ip:"+ul.getLoginiplog());
						//展示登陆时间
						session.setAttribute("admintime", "上次登陆时间:"+ul.getLogintimelog());
						//展示登陆次数
						session.setAttribute("admincounts",GetLoginLog.getLoginCounts(ul.getLogincountslog()));
					}else{
						System.out.println("添加日志失败!");
					}
				}
				session.setMaxInactiveInterval(15*60);//单位是秒，设置session时长为15分钟
				String timeHello = DateUtil.getTimeHello().concat("好！");
				//展示登陆时段
				session.setAttribute("timeHello", timeHello);
				
				session.setAttribute("adminuser", user1);
				writer.print("1");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//登陆成功，跳转主页面
	@RequestMapping(value="gomain",produces="text/html;charset=UTF-8")
	public String gomain(Model model,HttpServletRequest request) throws IOException{
		//System.out.println(request.getRequestURL()+" "+request.getLocalName()+" "+System.getProperty("os.name")+" "+System.getProperty("os.version")+" "+request.getProtocol()+" " +request.getCharacterEncoding());
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			return "forward:/jsps/main.jsp";
		}
	}
	//用户退出
	@RequestMapping("logout")
	public String logout(Model model,HttpServletRequest request){
		UserAdmin user= (UserAdmin) request.getSession().getAttribute("adminuser");
		if(user==null){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			request.getSession().removeAttribute("adminuser");
			model.addAttribute("ngworkusersuccess","退出成功！");
			return "success_logout";
		}
	} 
    
}
